package com.base.cn.platform.os.controller.course.cloud;

import com.base.cn.platform.os.common.constants.ConfigName;
import com.base.cn.platform.os.common.utils.TT.TTServiceUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.course.cloud.DemandEntity;
import com.base.cn.platform.os.entity.course.cloud.LiveEntity;
import com.base.cn.platform.os.entity.course.cloud.sms.CsySmsEntity;
import com.base.cn.platform.os.service.course.cloud.CloudServiceBiz;
import com.base.cn.platform.os.service.manage.config.SysConfigService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author zhanglei
 * @create 2018-08-21 下午 3:05
 */
@RestController
@RequestMapping("/course")
public class CloudServiceController {

    @Autowired
    private CloudServiceBiz cloudServiceBiz;
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private TTServiceUtils ttServiceUtils;

    /**
     * 录播
     * @param currentPage 页码
     * @param params 录播数据
     * @return
     */
    @RequestMapping("/findDemandPage")
    public PageInfo<DemandEntity> findDemandPage(@RequestParam("currentPage") Integer currentPage,
                                                 @RequestBody Map<String, Object> params)throws IOException {
        String name = (String)params.get("name");
        String status = (String)params.get("status");
        String fileType = (String)params.get("fileType");
        return cloudServiceBiz.demand(currentPage,name,status,fileType);
    }

    /**
     * 直播
     * @param currentPage 当前页
     * @param params 直播数据
     * @return
     */
    @RequestMapping("/findLivePage")
    public PageInfo<LiveEntity> findLivePage(@RequestParam("currentPage") Integer currentPage,
                                             @RequestBody Map<String, Object> params)throws IOException{
        String name = (String)params.get("name");
        String status = (String)params.get("status");
        Integer type = Integer.valueOf(params.get("type").toString());
        return cloudServiceBiz.live(currentPage,name,status,type);
    }


    /**
     * 直播
     * @param currentPage 当前页
     * @return
     */
    @RequestMapping("/findCCLivePage")
    public PageInfo<Map> findCCLivePage(@RequestParam("currentPage") Integer currentPage)throws IOException{
        return cloudServiceBiz.ccLive(currentPage);
    }

    @RequestMapping("/findTTLivePage")
    public PageInfo<Map> findTTLivePage(@RequestParam("currentPage") Integer currentPage)throws IOException{
        return ttServiceUtils.findTTLivePage(currentPage);
    }

    /**
     * 删除录播
     *
     * @param id_varchar 录播播放码
     */
    @RequestMapping("/deleteDemand")
    public Map<String,Object> deleteDemand(@RequestParam("id_varchar") String id_varchar)throws IOException{
        return cloudServiceBiz.delete(id_varchar);
    }

    /**
     * 关闭直播
     *
     * @param room_id 直播房间id
     */
    @RequestMapping("/closeLive")
    public Map<String,Object> closeLive(@RequestParam("room_id") String room_id,@RequestParam("type") Integer type)throws IOException{
        return cloudServiceBiz.close(room_id,type);
    }


    /**
     * 开通云视频服务
     * @param params 录播数据
     * @return
     */
    @RequestMapping("/openVideoCloudService")
    public Map<String,Object> openVideoCloudService(
                                                 @RequestBody Map<String, String> params) throws IOException {
        TreeMap<String,String> vlasProviderMap = sysConfigService.getSysConfigContext(ConfigName.VLASPROVIDER);
        if(!vlasProviderMap.get("type").equals("96K")){
            return ResultUtil.ERROR("不可申请开能云视频服务");
        }
        return cloudServiceBiz.openService(params);
    }

    /**
     * 获取短信数据
     * @return
     */
    @RequestMapping("/querySmsStatis")
    public PageInfo<CsySmsEntity> querySmsStatis(@RequestBody Map<String, Object> params,@RequestParam("type") int type) throws IOException {
        return cloudServiceBiz.querySmsStatis(params,type);
    }


    /**
     *
     * @param roomId
     * @return
     * @throws IOException
     */
    @RequestMapping("/findCCPlaybackPage")
    public List<Map> findCCPlaybackPage(@RequestParam("roomId") String roomId)throws IOException{
        return  cloudServiceBiz.findCCPlaybackPage(roomId);
    }


}
